/*
 * @Description:
 * @Author: mmzhang
 * @Date: 2021-10-18 14:46:31
 * @LastEditTime: 2022-01-25 17:02:08
 */
import fitClassApi from '@/config/server/api/fitClass'
import channelApi from '@/config/server/api/base.js'
import server from '@dt/server/dt'
import dt from '@dt/dt'

let channelListDrop = {
  clueChannelList: [],
  buildingChannelList: []
}

function channelList(info) {
  return channelApi.channel.channelList.fetch(info).then((res) => {
    return res
  })
}

function channelInsert(info) {
  return channelApi.channel.channelInsert.fetch(info).then((res) => {
    return res
  })
}

function channelUpdate(info) {
  return channelApi.channel.channelUpdate.fetch(info).then((res) => {
    return res
  })
}

function channelDelete(info) {
  return channelApi.channel.channelDelete.fetch(info).then((res) => {
    return res
  })
}

function caseChannelList(info) {
  return channelApi.caseChannel.caseChannelList.fetch(info).then((res) => {
    // console.log(res, '浮动啊');
    res.list.forEach((ele) => {
      ele.channelType = ele.channelType ? ele.channelType.toString() : ''
      // if (ele.cooperationType != 6) {
      //   ele.channelCostValue = Number(ele.channelCostValue)
      // }
      // if (ele.channelCostType == 3) {
      //   ele.channelCostValue = '浮动'
      // }
      // if (ele.relationClueChannel == 10008) {
      //   ele.channelCostValue = '浮动'
      // }
    })
    return res
  })
}

function caseChannelList1(info) {
  info.buildingId = info.buildingId ? info.buildingId : dt.buildInfo().housesId
  info.rows = 100000
  return channelApi.caseChannel.caseChannelList.fetch(info).then((res) => {
    return res
  })
}

function caseChannelListOrder(info) {
  console.log(info, 'iiiiiiiiiiiiiiiiiiiiiii')
  info.buildingId = info.buildingId ? info.buildingId : dt.buildInfo().housesId
  return channelApi.caseChannel.caseChannelTreeList.fetch(info).then((res) => {
    return res
  })
}

function caseChannelInsert(info) {
  return channelApi.caseChannel.caseChannelInsert.fetch(info).then((res) => {
    return res
  })
}

function caseChannelUpdate(info) {
  return channelApi.caseChannel.caseChannelUpdate.fetch(info).then((res) => {
    return res
  })
}

function caseChannelDelete(info) {
  return channelApi.caseChannel.caseChannelDelete.fetch(info).then((res) => {
    return res
  })
}

function caseChannelTreeList(buildingId) {
  return channelApi.caseChannel.caseChannelTreeList
    .fetch({
      buildingId,
      type: 1
    })
    .then((res) => {
      let data = res.map((item) => {
        let children = item.children.map((i) => {
          return {
            ...i,
            value: i.channelId,
            label: i.channelName
          }
        })
        return {
          value: item.channelType,
          label: item.channelTypeName,
          children
        }
      })
      return data
    })
}

// 线索渠道
function getClueChannelList(params) {
  return channelApi.system.clueChannelList.fetch(params).then((res) => {
    return res
  })
}

function changeStartOrOff(params) {
  return channelApi.system.startOrOff.fetch(params).then((res) => {
    return res
  })
}

// 是否显示关联提示

function isShowRelationTip(params) {
  return channelApi.caseChannel.isShowRelationTip.fetch(params).then((res) => {
    return res
  })
}

function atOnceRelationFun(params) {
  return channelApi.caseChannel.atOnceRelation.fetch(params).then((res) => {
    return res
  })
}

function getClueChannelRelationList(params) {
  return channelApi.caseChannel.clueChannelRelationList.fetch(params).then((res) => {
    return res
  })
}

// 渠道合集
function getClueRelationList(params) {
  return channelApi.caseChannel.clueRelationList.fetch(params).then((res) => {
    // 转换后的二维数组
    var arrayChannel = Object.values(
      res.buildingChannelList.reduce((res, item) => {
        res[item.channelType] ? res[item.channelType].push(item) : (res[item.channelType] = [item])
        return res
      }, {})
    )

    let data = []
    arrayChannel.forEach((item) => {
      let channelObj = {
        value: item[0].channelType,
        label: item[0].channelTypeName,
        children: []
      }
      item.forEach((itemChild) => {
        channelObj.children.push({
          value: itemChild.channelId,
          label: itemChild.channelName
        })
      })
      data.push(channelObj)
    })

    let clueData = []
    res.clueChannel.forEach((ele) => {
      clueData.push({
        key: ele.id,
        value: ele.channelName
      })
    })

    channelListDrop = {
      clueChannelList: clueData,
      buildingChannelList: data
    }
    return channelListDrop
  })
}

function importChannelData() {
  return channelListDrop
}
// 新增关联
function addClueRelation(params) {
  return channelApi.caseChannel.clueRelation.fetch(params).then((res) => {
    return res
  })
}

// 取消关联
function unClueRelationFun(params) {
  return channelApi.caseChannel.unClueRelation.fetch(params).then((res) => {
    return res
  })
}

//
function selectBaseChannelMergeList(params) {
  return channelApi.caseChannel.selectBaseChannelMergeList.fetch(params)
}

function mergeChannel(params) {
  return channelApi.caseChannel.mergeChannel.fetch(params)
}
// 自获客打法
function planChannelChannelList(params) {
  params.buildingId = dt.buildInfo().housesId
  return fitClassApi.planChannel.channelList.fetch(params).then((res) => {
    return res
  })
}

function channleAnnexLogList(params) {
  return channelApi.caseChannel.channleAnnexLogList.fetch(params)
}

export default {
  channelList,
  channelInsert,
  channelUpdate,
  channelDelete,
  caseChannelList,
  caseChannelList1,
  caseChannelListOrder,
  caseChannelInsert,
  caseChannelUpdate,
  caseChannelDelete,
  caseChannelTreeList,
  getClueChannelList,
  changeStartOrOff,
  isShowRelationTip,
  atOnceRelationFun,
  getClueChannelRelationList,
  getClueRelationList,
  importChannelData,
  addClueRelation,
  unClueRelationFun,
  selectBaseChannelMergeList,
  mergeChannel,
  planChannelChannelList,
  channleAnnexLogList
}
